package com.pinganfu.mock.test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpsUtil {

    private static class TrustAnyTrustManager implements X509TrustManager {

        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    /**
     * post方式请求服务器(https协议)
     * 
     * @param url
     *            请求地址
     * @param content
     *            参数
     * @param charset
     *            编码
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @throws IOException
     */
    public static byte[] post(String url, String content, String charset)
            throws NoSuchAlgorithmException, KeyManagementException,
            IOException {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                new java.security.SecureRandom());

        URL console = new URL(url);
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
        conn.setSSLSocketFactory(sc.getSocketFactory());
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
        conn.setDoOutput(true);
        conn.connect();
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.write(content.getBytes(charset));
        // 刷新、关闭
        out.flush();
        out.close();
        InputStream is = conn.getInputStream();
        if (is != null) {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            is.close();
            return outStream.toByteArray();
        }
        return null;
    }

    
    public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, IOException {
    	String sh = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>"
				+ "<reqId>合作伙伴编号</reqId>"
				+ "<serviceId>1000010000000070</serviceId>"
				+ "<bizdata>"
				+ "<sign>"
				+ "<idx>00</idx>"
				+ "<value>签名值</value>"
				+ "</sign>"
				+ "<data>"
				+ "<encode>utf-8</encode>"
				+ "<LPMS_Pionts>"
				+ "<Request>"
				+ "<PropertySet>"
				+ "<SiebelMessage>"
				+ "<ProcessName>PA UpdTicket Process</ProcessName>"
				+ "<TerminalID></TerminalID>"
				+ "<param></param>"
				+ "<FirmID></FirmID>"
				+ "<StoreID></StoreID>"
				+ "<Operation>UpdTicketOp</Operation>"
				+ "<Method>UpdTicket</Method>"
				+ "<TransactionType></TransactionType>"
				+ "<trip>"
				+ "<departure>TAO</departure>"
				+ "<departureCn>青岛</departureCn>"
				+ "<destination>PVG</destination>"
				+ "<destinationCn>上海浦东</destinationCn>"
				+ "<ticketNo>7815895715828</ticketNo>"
				+ "<passagerName>孙勇</passagerName>"
				+ "<sequence>1</sequence>"
				+ "<usageStatus>USED/FLOWN/</usageStatus>"
				+ "<usageStatusCn>已使用</usageStatusCn>"
				+ "</trip>"
				+ "<trip></trip>"
				+ "</SiebelMessage>"
				+ "</PropertySet>"
				+ "</Request>"
				+ "</LPMS_Pionts>"
				+ "</data>"
				+ "</bizdata></root>";
    	
    	String url = "https://jk-bis-stg.dmzstg.pingan.com.cn:7443/bis/merService";
    	post(url, sh, "UTF-8");
	}
}
